package com.terrynoya.fight.char
{
	import com.terrynoya.common.util.DisplayUtil;
	import com.terrynoya.fight.animations.AnimationManager;
	import com.terrynoya.fight.animations.movieClip.MCAnimationAssets;
	import com.terrynoya.fight.emuns.Facing;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;

	public class MCCharacterRender implements ICharaterRender
	{
		private var _aniAssets:MCAnimationAssets;
		
		private var _render:Sprite;
		
		private var _currentMc:MovieClip;
		
		public function MCCharacterRender(aniAssets:MCAnimationAssets)
		{
			this._render = new Sprite();
			this._aniAssets = aniAssets;
		}
		
		public function get rendered():DisplayObject
		{
			return this._render;
		}
		
		public function draw(character:Entity,view:Sprite):void
		{
			var aniManager:AnimationManager = character.animationManager;
			var mc:MovieClip = this._aniAssets.getMovieClip(aniManager.currentAni.id);
			if(mc != _currentMc)
			{
				DisplayUtil.clearChildren(this._render);
				this._render.addChild(mc);
				this._currentMc = mc;
			} 
			mc.gotoAndStop(aniManager.currentElement.id);
			this._render.x = character.position.x;
			this._render.y = character.position.y;
			this._render.scaleX = character.facing == Facing.RIGHT ? 1 : -1;
			this._render.y = character.position.y + character.engine.stage.height;	
		}
	}
}